package cqu.zhaiyixin20215521.huarondao.slice.pictureSelect;

import cqu.zhaiyixin20215521.huarondao.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.*;

import java.util.ArrayList;
import java.util.List;

public class ImageListProvider extends BaseItemProvider {
    private final List<Integer> list;
    private final AbilitySlice slice;
    private final Component.ClickedListener clickedListener;

    public ImageListProvider(AbilitySlice slice, int[] imageResourceIDs, Component.ClickedListener l) {
        this.slice = slice;
        this.list = new ArrayList<>();
        for(int id: imageResourceIDs) {
            list.add(id);
        }
        this.clickedListener = l;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        Component cpt;
        if(component == null) {
            cpt = LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_item_image, null, false);
        } else {
            cpt = component;
        }
        Image image = cpt.findComponentById(ResourceTable.Id_ItemImage);
        image.setPixelMap(list.get(i));
        image.setClickedListener(clickedListener);
        return cpt;
    }
}
